﻿namespace HRM.CORE
{
    using DevExpress.Utils;
    using DevExpress.XtraBars;
    using DevExpress.XtraEditors;
    using DevExpress.XtraEditors.Repository;
    using DevExpress.XtraGrid.Views.BandedGrid;
    using APP.HELPER;
    using Common.Class; using APP.HELPER.Utils.Language; using Security.Security; using Dictionary.ERP;
    using HRM.CORE;
    using System; using Extra.ERP;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    using System.Windows.Forms; using Common;
    using Extra.ERP;
    using Security.ERP;

    public class xucTimekeepingExtra : Common.xucBaseBasic
    {
        private IContainer components = null;
        private System.DateTime m_Date;
        private string m_EmployeeCode;
        private string m_ShiftCode;
        private Guid m_TimeKeeperTableListID;

        public event AddedEventHander Added;

        public event DeletedEventHander Deleted;

        public event UpdatedEventHander Updated;

        public xucTimekeepingExtra()
        {
            this.InitializeComponent();
            base.ucToolBar.SetInterface();
        }

        protected override void Add()
        {
            xfmTimekeepingExtraAdd add = new xfmTimekeepingExtraAdd(Actions.Add, this.m_TimeKeeperTableListID, this.m_EmployeeCode, this.m_ShiftCode, this.m_Date);
            add.Added += new xfmTimekeepingExtraAdd.AddedEventHander(this.frm_Added);
            add.ShowDialog();
        }

        private void AddRow(HRM_TIMEKEEPER_EXTRA Item)
        {
            AdvBandedGridView gbList = base.gbList;
            int focusedRowHandle = gbList.FocusedRowHandle;
            gbList.AddNewRow();
            focusedRowHandle = gbList.FocusedRowHandle;
            gbList.SetRowCellValue(focusedRowHandle, "OrderID", Item.OrderID);
            gbList.SetRowCellValue(focusedRowHandle, "ExtraBeginTime", Item.ExtraBeginTime);
            gbList.SetRowCellValue(focusedRowHandle, "ExtraEndTime", Item.ExtraEndTime);
            gbList.SetRowCellValue(focusedRowHandle, "Hour", Item.Hour);
            gbList.SetRowCellValue(focusedRowHandle, "Minute", Item.Minute);
            gbList.UpdateCurrentRow();
            this.RaiseAddedEventHander(Item);
        }

        public override void Change()
        {
            HRM_TIMEKEEPER_EXTRA item = new HRM_TIMEKEEPER_EXTRA();
            object cellValue = base.GetCellValue(base.m_RowClickEventArgs.RowIndex, "OrderID");
            if (cellValue != null)
            {
                base.SetWaitDialogCaption("Đang kiểm tra dữ liệu....");
                if (item.Get(this.m_TimeKeeperTableListID, this.m_EmployeeCode, this.m_ShiftCode, this.m_Date, new Guid(cellValue.ToString())) != "OK")
                {
                    this.DoHide();
                    XtraMessageBox.Show("Dữ liệu kh\x00f4ng tồn tại", "Th\x00f4ng B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else
                {
                    this.DoHide();
                    xfmTimekeepingExtraAdd add = new xfmTimekeepingExtraAdd(Actions.Update, item);
                    add.Updated += new xfmTimekeepingExtraAdd.UpdatedEventHander(this.frm_Updated);
                    add.Added += new xfmTimekeepingExtraAdd.AddedEventHander(this.frm_Added);
                    add.ShowDialog();
                }
            }
        }

        public override void Delete()
        {
            if (!ClsOption.System2.IsQuestion || (XtraMessageBox.Show("Bạn c\x00f3 muốn x\x00f3a kh\x00f4ng?", "Th\x00f4ng b\x00e1o", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.No))
            {
                object cellValue;
                base.SetWaitDialogCaption("Đang x\x00f3a...");
                string str = "";
                bool flag = false;
                AdvBandedGridView gbList = base.gbList;
                int[] selectedRows = gbList.GetSelectedRows();
                HRM_TIMEKEEPER_EXTRA hrm_timekeeper_extra = new HRM_TIMEKEEPER_EXTRA();
                for (int i = selectedRows.Length; i > 0; i--)
                {
                    flag = true;
                    cellValue = base.GetCellValue(selectedRows[i - 1], "OrderID");
                    if (cellValue != null)
                    {
                        SYS_LOG.Insert("Tăng Cường Sau Ca L\x00e0m Việc", "Xo\x00e1", cellValue.ToString());
                        str = hrm_timekeeper_extra.Delete(this.m_TimeKeeperTableListID, this.m_EmployeeCode, this.m_ShiftCode, this.m_Date, new Guid(cellValue.ToString()));
                        if (str == "OK")
                        {
                            gbList.DeleteRow(selectedRows[i - 1]);
                        }
                        else if (str != "OK")
                        {
                            MessageBox.Show("Th\x00f4ng tin kh\x00f4ng được x\x00f3a\n" + str, "Th\x00f4ng b\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                        }
                    }
                }
                this.DoHide();
                if (!flag)
                {
                    if (gbList.DataSource == null)
                    {
                        return;
                    }
                    RowClickEventArgs args = new RowClickEventArgs((gbList == null) ? -1 : gbList.FocusedRowHandle, (gbList.FocusedColumn == null) ? -1 : gbList.FocusedColumn.ColumnHandle, (gbList.FocusedColumn == null) ? "" : gbList.FocusedColumn.FieldName);
                    base.m_RowClickEventArgs = args;
                    cellValue = null;
                    cellValue = base.GetCellValue(args.RowIndex, "OrderID");
                    if (cellValue == null)
                    {
                        return;
                    }
                    SYS_LOG.Insert("Tăng Cường Sau Ca L\x00e0m Việc", "Xo\x00e1", cellValue.ToString());
                    base.SetWaitDialogCaption("Đang x\x00f3a...");
                    str = hrm_timekeeper_extra.Delete(this.m_TimeKeeperTableListID, this.m_EmployeeCode, this.m_ShiftCode, this.m_Date, new Guid(cellValue.ToString()));
                    if (str == "OK")
                    {
                        gbList.DeleteRow(args.RowIndex);
                    }
                    else if (str != "OK")
                    {
                        MessageBox.Show("Th\x00f4ng tin kh\x00f4ng được x\x00f3a\n" + str, "Th\x00f4ng b\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    }
                    this.DoHide();
                }
                this.RaiseDeletedEventHander();
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        protected override bool ExportPermision()
        {
            SYS_LOG.Insert("Tăng Cường Sau Ca L\x00e0m Việc", "Xuất");
            return base.ExportPermision();
        }

        private void frm_Added(object sender, HRM_TIMEKEEPER_EXTRA e)
        {
            this.AddRow(e);
        }

        private void frm_Updated(object sender, HRM_TIMEKEEPER_EXTRA Item)
        {
            this.UpdateRow(Item, base.m_RowClickEventArgs);
        }

        protected override void gbList_MouseDown(object sender, MouseEventArgs e)
        {
            AdvBandedGridView view = (AdvBandedGridView) sender;
            RowClickEventArgs args = new RowClickEventArgs((view == null) ? -1 : view.FocusedRowHandle, (view.FocusedColumn == null) ? -1 : view.FocusedColumn.ColumnHandle, (view.FocusedColumn == null) ? "" : view.FocusedColumn.FieldName);
            base.m_RowClickEventArgs = args;
            object cellValue = base.GetCellValue(args.RowIndex, "ShiftCode");
            base.DisableMenu(false);
            if (cellValue == null)
            {
                base.DisableMenu(true);
            }
        }

        private void InitializeComponent()
        {
            this.components = new Container();
            base.AutoScaleMode = AutoScaleMode.Font;
        }

        protected override void List_Init(AdvBandedGridView dt)
        {
            RepositoryItemTimeEdit edit = new RepositoryItemTimeEdit {
                EditMask = "HH:mm tt dd/MM/yyyy"
            };
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                string fieldName = dt.Columns[i].FieldName;
                if (fieldName == null)
                {
                    goto Label_0452;
                }
                if (!(fieldName == "OrderID"))
                {
                    if (fieldName == "ExtraBeginTime")
                    {
                        goto Label_013E;
                    }
                    if (fieldName == "ExtraEndTime")
                    {
                        goto Label_0217;
                    }
                    if (fieldName == "Hour")
                    {
                        goto Label_02F0;
                    }
                    if (fieldName == "Minute")
                    {
                        goto Label_03AC;
                    }
                    goto Label_0452;
                }
                dt.Columns[i].OptionsColumn.ReadOnly = true;
                dt.Columns[i].OptionsColumn.AllowEdit = false;
                dt.Columns[i].OptionsColumn.AllowGroup = DefaultBoolean.False;
                dt.Columns[i].OptionsColumn.FixedWidth = true;
                if (SysOption.Language == "VN")
                {
                    dt.Columns[i].Caption = "M\x00e3";
                }
                dt.Columns[i].Width = 80;
                dt.Columns[i].Visible = false;
                continue;
            Label_013E:
                dt.Columns[i].OptionsColumn.ReadOnly = true;
                dt.Columns[i].OptionsColumn.AllowEdit = false;
                dt.Columns[i].OptionsColumn.AllowGroup = DefaultBoolean.False;
                dt.Columns[i].AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
                dt.Columns[i].ColumnEdit = edit;
                dt.Columns[i].OptionsColumn.FixedWidth = true;
                if (SysOption.Language == "VN")
                {
                    dt.Columns[i].Caption = "Bắt đầu";
                }
                dt.Columns[i].Width = 180;
                continue;
            Label_0217:
                dt.Columns[i].OptionsColumn.ReadOnly = true;
                dt.Columns[i].OptionsColumn.AllowEdit = false;
                dt.Columns[i].OptionsColumn.AllowGroup = DefaultBoolean.False;
                dt.Columns[i].AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
                dt.Columns[i].ColumnEdit = edit;
                dt.Columns[i].OptionsColumn.FixedWidth = true;
                if (SysOption.Language == "VN")
                {
                    dt.Columns[i].Caption = "Kết th\x00fac";
                }
                dt.Columns[i].Width = 180;
                continue;
            Label_02F0:
                dt.Columns[i].OptionsColumn.ReadOnly = true;
                dt.Columns[i].OptionsColumn.AllowEdit = false;
                dt.Columns[i].OptionsColumn.AllowGroup = DefaultBoolean.False;
                dt.Columns[i].ColumnEdit = edit;
                dt.Columns[i].OptionsColumn.FixedWidth = true;
                if (SysOption.Language == "VN")
                {
                    dt.Columns[i].Caption = "Số giờ";
                }
                dt.Columns[i].Width = 180;
                continue;
            Label_03AC:
                dt.Columns[i].OptionsColumn.ReadOnly = true;
                dt.Columns[i].OptionsColumn.AllowEdit = false;
                dt.Columns[i].OptionsColumn.AllowGroup = DefaultBoolean.False;
                dt.Columns[i].OptionsColumn.FixedWidth = true;
                if (SysOption.Language == "VN")
                {
                    dt.Columns[i].Caption = "Số ph\x00fat";
                }
                dt.Columns[i].Width = 150;
                continue;
            Label_0452:
                dt.Columns[i].Visible = false;
            }
        }

        protected override void Print()
        {
            SYS_LOG.Insert("Tăng Cường Sau Ca L\x00e0m Việc", "In");
            base.Print();
        }

        public void RaiseAddedEventHander(HRM_TIMEKEEPER_EXTRA Item)
        {
            if (this.Added != null)
            {
                this.Added(this, Item);
            }
        }

        public void RaiseDeletedEventHander()
        {
            if (this.Deleted != null)
            {
                this.Deleted(this, base.m_RowClickEventArgs);
            }
        }

        public void RaiseUpdatedEventHander(HRM_TIMEKEEPER_EXTRA Item)
        {
            if (this.Updated != null)
            {
                this.Updated(this, Item);
            }
        }

        public override void ReLoad()
        {
            base.SetWaitDialogCaption("Đang nạp dữ liệu...");
            base.gcList.DataSource = new HRM_TIMEKEEPER_EXTRA().GetList(this.m_TimeKeeperTableListID, this.m_EmployeeCode, this.m_ShiftCode, this.m_Date);
            base.SetWaitDialogCaption("Đang nạp cấu h\x00ecnh...");
            this.List_Init(base.gbList);
            base.SetWaitDialogCaption("Nạp quyền sử dụng...");
            MyRule.Get(MyLogin.RoleId, "bbiShift");
            if (!MyRule.AllowPrint)
            {
                base.ucToolBar.bbiPrint.Visibility = BarItemVisibility.Never;
            }
            MyRule.Get(MyLogin.RoleId, "bbiShift");
            if (!MyRule.AllowExport)
            {
                base.ucToolBar.bbiExport.Visibility = BarItemVisibility.Never;
            }
            MyRule.Get(MyLogin.RoleId, "bbiShift");
            if (!MyRule.AllowAdd)
            {
                base.ucToolBar.bbiAdd.Visibility = BarItemVisibility.Never;
            }
            MyRule.Get(MyLogin.RoleId, "bbiShift");
            if (!MyRule.AllowDelete)
            {
                base.ucToolBar.bbiDelete.Visibility = BarItemVisibility.Never;
            }
            MyRule.Get(MyLogin.RoleId, "bbiShift");
            if (!MyRule.AllowEdit)
            {
                base.ucToolBar.bbiEdit.Visibility = BarItemVisibility.Never;
            }
            base.SetWaitDialogCaption("Đ\x00e3 xong...");
            this.DoHide();
        }

        public void SetData(Guid TimeKeeperTableListID, string EmployeeCode, string ShiftCode, System.DateTime Date)
        {
            this.m_TimeKeeperTableListID = TimeKeeperTableListID;
            this.m_EmployeeCode = EmployeeCode;
            this.m_ShiftCode = ShiftCode;
            this.m_Date = Date;
        }

        private void UpdateRow(HRM_TIMEKEEPER_EXTRA Item, RowClickEventArgs e)
        {
            AdvBandedGridView gbList = base.gbList;
            int rowIndex = e.RowIndex;
            gbList.SetRowCellValue(rowIndex, "OrderID", Item.OrderID);
            gbList.SetRowCellValue(rowIndex, "ExtraBeginTime", Item.ExtraBeginTime);
            gbList.SetRowCellValue(rowIndex, "ExtraEndTime", Item.ExtraEndTime);
            gbList.SetRowCellValue(rowIndex, "Hour", Item.Hour);
            gbList.SetRowCellValue(rowIndex, "Minute", Item.Minute);
            gbList.UpdateCurrentRow();
            this.RaiseUpdatedEventHander(Item);
        }

        private void xucCustomerType_Load(object sender, EventArgs e)
        {
        }

        public delegate void AddedEventHander(object sender, HRM_TIMEKEEPER_EXTRA Item);

        public delegate void DeletedEventHandler(object sender, RowClickEventArgs e);

        public delegate void UpdatedEventHander(object sender, HRM_TIMEKEEPER_EXTRA Item);
    }
}

